package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.PacPatentInfo;
import com.baitengsoft.patentapimonitor.api.model.dto.WebApplicantTypeInfoDto;
import com.baitengsoft.patentapimonitor.api.model.dto.WebPacPatentInfoDto;
import com.baitengsoft.patentapimonitor.api.model.param.SearchPatentParam;
import com.baitengsoft.patentapimonitor.api.service.PacPatentinfoService;
import com.baitengsoft.patentapimonitor.api.service.WebExcelExportService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Chen 2018/1/11
 */
@RestController
@RequestMapping("/api/v1/web/analysis/patentSearch")
@Api(value = "前台-统计分析-专利检索",description = "专利检索")
public class WebPatSearchController extends AbstractController {
    @Autowired
    private PacPatentinfoService pacPatentinfoService;
    @Autowired
    private WebExcelExportService webExcelExportService;


    @ApiOperation(value = "专利检索")
    @GetMapping("/search")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "areaId", value = "区域ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patType", value = "类型ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patHubTypeName", value = "类型Name", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "lawState", value = "法律状态", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patNum", value = "申请号", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patName", value = "专利名称", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applyPerson", value = "权利人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "inventPerson", value = "发明人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "agencyName", value = "代理机构", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "address", value = "地址", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "shenqingriStart", value = "申请日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "shenqingriEnd", value = "申请日结束", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "gkrStart", value = "公开日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "gkrEnd", value = "公开日结束", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "ssrStart", value = "实审日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "ssrEnd", value = "实审日结束", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sxrStart", value = "失效日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sxrEnd", value = "失效日结束", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "shouquanriStart", value = "授权日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "shouquanriEnd", value = "授权日结束", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orderby", value = "排序字段," +
                    "授权日值为PatApproveDate，申请日值为PatAppDate",
                    required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "正序倒序，DESC为倒序，ASC为正序",
                    required =
                    false),
    })
    public Res searchPatList(@ApiIgnore SearchPatentParam param) {
        if (param.getPageNumber() == null || param.getPageSize() == null) {
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null) {
            return Res.error("登录信息失效");
        }
        PageResult<PacPatentInfo> r = pacPatentinfoService.searchByParam(param, userInfo.getPlatformid());
        return Res.ok().put("list", r);
    }

    @ApiOperation(value = "专利检索导出")
    @GetMapping("/searchexport")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "areaId", value = "区域ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patType", value = "类型ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "lawState", value = "法律状态", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patNum", value = "申请号", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patName", value = "专利名称", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applyPerson", value = "权利人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "inventPerson", value = "发明人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "agencyName", value = "代理机构", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "address", value = "地址", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "shenqingriStart", value = "申请日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "shenqingriEnd", value = "申请日结束", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "gkrStart", value = "公开日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "gkrEnd", value = "公开日结束", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "ssrStart", value = "实审日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "ssrEnd", value = "实审日结束", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sxrStart", value = "失效日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sxrEnd", value = "失效日结束", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "shouquanriStart", value = "授权日开始", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "shouquanriEnd", value = "授权日结束", required = false),
    })
    public Res exportExcel(@ApiIgnore SearchPatentParam param, HttpServletResponse response) {
        if (param.getPageNumber() == null || param.getPageSize() == null) {
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null) {
            return Res.error("登录信息失效");
        }
        if (param.getPageSize()>5000)   return Res.error("最多可导出5000条数据");
        PageResult<PacPatentInfo> pacPatentInfoPageResult = pacPatentinfoService.searchByParam(param, userInfo.getPlatformid());
        if (pacPatentInfoPageResult == null || pacPatentInfoPageResult.getItems() == null || pacPatentInfoPageResult.getItems().size() < 1) {
            return Res.error("无数据");
        }
        List<PacPatentInfo> items = pacPatentInfoPageResult.getItems();
        List<WebPacPatentInfoDto> webDtos = webExcelExportService.getWebPacPatentInfoDto(items);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "专利检索数据.xls",
                "专利检索数据");
    }
}